import keymaster from 'keymaster';
const delay = ms =>new Promise(function(resolve){
  setTimeout(function(){
    resolve();
  },ms);
});

export default {

  namespace: 'counter',

  state: {
    current:0,
    record:0
  },
  reducers: {
    add(state, action) {
      let current = state.current+1;
      return { ...state, 
        current,
        record:current>state.record?current:state.record
      };
    },
    minus(state){
      let current = state.current-1;
      return { ...state, 
        current
      };
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      keymaster('space',function(){
        dispatch({type:'add'});
      });
    },
  },

  effects: {
    *add({ payload }, {call,put}) {
      yield call(delay,1000);
      yield put({ type: 'minus' });
    },
  },

  

};
